package com.hna.eking.Controller;

import java.util.List;

import org.mybatis.spring.MyBatisSystemException;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hna.eking.Exception.RunTimeException;
import  com.hna.eking.iBatis.TableItemInfoImp;
import com.hna.eking.iBatis.TableItemInfoMapper;

@RestController
@MapperScan("com.hna.eking.iBatis")
public class GetTableItemInfo {
	@Autowired
	private TableItemInfoImp TableItem;
	private static final Logger logger = LoggerFactory.getLogger(GetTableItemInfo.class);
	@RequestMapping(value = "/TableItemInfo", method=RequestMethod.GET)
	public String GetTableItemInfo(@RequestParam(value="TableName") String TableName) {
		JSONObject json = new JSONObject();
		if(TableName == null || "".equals(TableName)){
			json.put("ErrorCode", RunTimeException.WEB_CONTROL_STATUS_PARM_NULL);
			return json.toJSONString();
		}
		try{
			List<TableItemInfoMapper> TableItemInfo =  TableItem.getByTableName(TableName);
			if(TableItemInfo.size() != 0){
				
				json.put("ErrorCode", RunTimeException.WEB_CONTROL_STATUS_SUCC);				
				json.put("TableName", TableItemInfo.get(0).getTableName());
				json.put("ColumnName", TableItemInfo.get(0).getColumn_Name());
				json.put("ColumnAlias", TableItemInfo.get(0).getColumn_Alias());
				json.put("ColumnType", TableItemInfo.get(0).getColumn_Type());
				return json.toJSONString();
			}else{
				logger.debug("data not found");
				json.put("ErrorCode", RunTimeException.WEB_DATA_NOT_FOUND);
				return json.toJSONString();
			}
			
		}catch(MyBatisSystemException e){
			logger.error("mybatis error" + e.getMessage());
			json.put("ErrorCode", RunTimeException.MAIN_SQL_ERROR_EXCEPTION);
			return json.toJSONString();
		}		
	}
	
	@RequestMapping(value = "/TableItemInfo", method=RequestMethod.POST)
	public String PostTableItemInfo(@RequestParam(value="TableInfo") String TableInfo) {
		JSONObject json = JSONObject.parseObject(TableInfo);
		
		TableItemInfoMapper TableItemInfo = new TableItemInfoMapper();
		TableItemInfo.setTableName(json.getString("TableName"));
		TableItemInfo.setColumn_Name(json.getString("ColumnName"));
		TableItemInfo.setColumn_Type(json.getString("ColumnType"));
		TableItemInfo.setColumn_Alias(json.getString("ColumnAlias"));
		int iRet = TableItem.InsertTableItem(TableItemInfo);
		JSONObject ret = new JSONObject();
		ret.put("ErrorCode", RunTimeException.WEB_CONTROL_CREATE_SUCC);
		return ret.toJSONString();
	}
	
	@RequestMapping(value = "/TableItemInfo", method=RequestMethod.DELETE )
	public String DelTableItemInfo(@RequestParam(value="TableName") String TableName) {
		TableItem.DeleteTableItem(TableName);
		JSONObject ret = new JSONObject();
		ret.put("ErrorCode", RunTimeException.WEB_CONTROL_DETE_SUCC);
		return ret.toJSONString();
	}
	
}
